home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Macintosh Drag and Drop / Demo Applications / Dragster / DragText Sources / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  4.5 KB  |  238 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        file.c
  4.  *
  5.  *        File Input/Output routines.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Thursday, January 26, 1992
  10.  *
  11.  *        12/30/94    JML        Code now compiles with Universal interfaces.
  12.  *
  13.  *        Copyright © 1992 Apple Computer, Inc.
  14.  *
  15.  */
  16.  
  17. #include <Errors.h>
  18. #include <ToolUtils.h>
  19. #include <Files.h>
  20. #include <StandardFile.h>
  21. #include "globals.h"
  22. #include "prototypes.h"
  23. #include "DTResources.h"
  24.  
  25.  
  26. short ReadDocumentFile(Document *theDocument)
  27.  
  28. {    long        count;
  29.     short        theResult;
  30.     char        buffer[256];
  31.     TextStyle    theStyle;
  32.  
  33.     SetCursor(*GetCursor(watchCursor));
  34.  
  35.     TESetSelect(0, (**(theDocument->theTE)).teLength, theDocument->theTE);
  36.     TEDelete(theDocument->theTE);
  37.  
  38.     if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
  39.         return(theResult);
  40.  
  41.     do {
  42.         count = 256;
  43.         theResult = FSRead(theDocument->fRefNum, &count, &buffer);
  44.         TEInsert(&buffer, count, theDocument->theTE);
  45.     } while (theResult == noErr);
  46.  
  47.     TESetSelect(0, 32767, theDocument->theTE);
  48.     theStyle.tsFont = 21;
  49.     theStyle.tsSize = 12;
  50.     TESetStyle(doFont + doSize, &theStyle, true, theDocument->theTE);
  51.     TESetSelect(0, 0, theDocument->theTE);
  52.     theDocument->dirty = false;
  53.  
  54.     return(noErr);
  55. }
  56.  
  57.  
  58. short WriteDocumentFile(Document *theDocument)
  59.  
  60. {    short        theResult;
  61.     long        length;
  62.     char        *bufPtr;
  63.  
  64.     SetCursor(*GetCursor(watchCursor));
  65.  
  66.     if (! theDocument->fRefNum)
  67.         return(fnOpnErr);
  68.  
  69.     if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
  70.         return(theResult);
  71.  
  72.     length = (**(theDocument->theTE)).teLength;
  73.     bufPtr = *((**(theDocument->theTE)).hText);
  74.  
  75.     if (theResult = FSWrite(theDocument->fRefNum, &length, bufPtr))
  76.         return(theResult);
  77.  
  78.     if (theResult = SetEOF(theDocument->fRefNum, length))
  79.         return(theResult);
  80. }
  81.  
  82.  
  83. void DoNewDocument()
  84.  
  85. {    Document        *theDocument;
  86.  
  87.     if (theDocument = NewDocument()) {
  88.         ShowWindow(theDocument->theWindow);
  89.     }
  90. }
  91.  
  92.  
  93. OSErr DoOpenFile(FSSpec *theFile)
  94.  
  95. {    OSErr            result;
  96.     short            refNum;
  97.     Document        *theDocument;
  98.  
  99.     if (result = FSpOpenDF(theFile, fsRdWrPerm, &refNum))
  100.         return(result);
  101.  
  102.     if (theDocument = NewDocument()) {
  103.         theDocument->fRefNum = refNum;
  104.         if (ReadDocumentFile(theDocument)) {
  105.             SysBeep(1);
  106.         }
  107.         SetWTitle(theDocument->theWindow, theFile->name);
  108.         AdjustScrollBar(theDocument);
  109.         ShowWindow(theDocument->theWindow);
  110.     } else {
  111.         SysBeep(1);
  112.         FSClose(refNum);
  113.         return(memFullErr);
  114.     }
  115.     return(noErr);
  116. }
  117.  
  118.  
  119.  
  120. void DoOpenDocument()
  121.  
  122. {    short                refNum;
  123.     SFTypeList            theTypeList;
  124.     StandardFileReply    theReply;
  125.     Document            *theDocument;
  126.  
  127.     theTypeList[0] = 'TEXT';
  128.     StandardGetFile(0L, 1, theTypeList, &theReply);
  129.  
  130.     if (theReply.sfGood) {
  131.         DoOpenFile(&theReply.sfFile);
  132.  
  133. /*        if (FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &refNum)) {
  134.             SysBeep(1);
  135.             return;
  136.         }
  137.         if (theDocument = NewDocument()) {
  138.             theDocument->fRefNum = refNum;
  139.             if (ReadDocumentFile(theDocument)) {
  140.                 SysBeep(1);
  141.             }
  142.             SetWTitle(theDocument->theWindow, theReply.sfFile.name);
  143.             AdjustScrollBar(theDocument);
  144.             ShowWindow(theDocument->theWindow);
  145.         } else {
  146.             SysBeep(1);
  147.             FSClose(refNum);
  148.         }
  149. */
  150.  
  151.     }
  152. }
  153.  
  154.  
  155. short DoSaveAsDocument(Document *theDocument)
  156.  
  157. {    short                theResult;
  158.     Str255                thePrompt, theName;
  159.     StandardFileReply    theReply;
  160.  
  161.     if (! theDocument)
  162.         return(false);
  163.  
  164.     GetIndString(thePrompt, FileStringsID, slSavePromptIndex);
  165.     GetWTitle(theDocument->theWindow, theName);
  166.     StandardPutFile(thePrompt, theName, &theReply);
  167.  
  168.     if (theReply.sfGood) {
  169.         if (!theReply.sfReplacing) {
  170.             theResult = FSpCreate(&theReply.sfFile, FileCreator, FileType, theReply.sfScript);
  171.             if (theResult) {
  172.                 SysBeep(1);
  173.                 return(false);
  174.             }
  175.         }
  176.         if (theDocument->fRefNum) {
  177.             theResult = FSClose(theDocument->fRefNum);
  178.         }
  179.         theResult = FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &theDocument->fRefNum);
  180.         if (theResult) {
  181.             SysBeep(1);
  182.             return(false);
  183.         }
  184.  
  185.         if (theResult = WriteDocumentFile(theDocument)) {
  186.             SysBeep(1);
  187.             return(false);
  188.         }
  189.  
  190.         SetWTitle(theDocument->theWindow, theReply.sfFile.name);
  191.         theDocument->dirty = false;
  192.  
  193.     } else {
  194.         return(false);
  195.     }
  196.     return(true);
  197. }
  198.  
  199.  
  200. short DoSaveDocument(Document *theDocument)
  201.  
  202. {
  203.     if (! theDocument)
  204.         return(false);
  205.  
  206.     if (theDocument->fRefNum) {
  207.         if (WriteDocumentFile(theDocument)) {
  208.             SysBeep(1);
  209.             return(false);
  210.         } else {
  211.             theDocument->dirty = false;
  212.         }
  213.         return(true);
  214.     } else {
  215.         return(DoSaveAsDocument(theDocument));
  216.     }
  217. }
  218.  
  219.  
  220. void DoRevertDocument(Document *theDocument)
  221.  
  222. {    Str255        theName;
  223.  
  224.     if (! theDocument)
  225.         return;
  226.  
  227.     if (theDocument->fRefNum) {
  228.         GetWTitle(theDocument->theWindow, theName);
  229.         ParamText(theName, "\p", "\p", "\p");
  230.         if (Alert(idRevertALRT, 0L) == 1) {
  231.             if (ReadDocumentFile(theDocument))
  232.                 SysBeep(1);
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238.